package cn.edu.scau.cmi.qiuzeyang.controller;

import cn.edu.scau.cmi.qiuzeyang.dao.StudentDao;
import cn.edu.scau.cmi.qiuzeyang.dao.TeacherDao;
import cn.edu.scau.cmi.qiuzeyang.domain.Student;
import cn.edu.scau.cmi.qiuzeyang.domain.Teacher;
import cn.edu.scau.cmi.qiuzeyang.util.ApplicationContextUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;

public class Controller {
	@FXML
	private TextArea textArea;

	@FXML
	private Tab addTab;
	@FXML
	private Pane addPane;
	@FXML
	private Button addStuButton;
	@FXML
	private Button addTeaButton;
	@FXML
	private TextField addStuText;
	@FXML
	private TextField addTeaText;
	@FXML
	private Label addStuName;
	@FXML
	private Label addTeaName;
	@FXML

	private Tab delTab;
	@FXML
	private Pane delPane;
	@FXML
	private Label delStuName;
	@FXML
	private Label delTeaName;
	@FXML
	private ChoiceBox delStuChoice;
	@FXML
	private ChoiceBox delTeaChoice;
	@FXML
	private Button delStuButton;
	@FXML
	private Button delTeaButton;

	@FXML
	private Tab modTab;
	@FXML
	private Pane modPane;
	@FXML
	private Label modStuName;
	@FXML
	private Label modTeaName;
	@FXML
	private TextField modStuText;
	@FXML
	private TextField modTeaText;
	@FXML
	private Button modStuButton;
	@FXML
	private Button modTeaButton;
	@FXML
	private ChoiceBox modStuChoice;
	@FXML
	private ChoiceBox modTeaChoice;

	@FXML
	private Tab manTab;
	@FXML
	private Pane manPane;
	@FXML
	private Label manStuName;
	@FXML
	private Label manTeaName;
	@FXML
	private ChoiceBox manStuChoice;
	@FXML
	private ChoiceBox manTeaChoice;
	@FXML
	private Button addStuTea;
	@FXML
	private Button addTeaStu;
	@FXML
	private Button removeTeaStu;
	@FXML
	private Button updateStuTea;
	@FXML
	private Button showStu;

	private StudentDao studentDao;
	private TeacherDao teacherDao;

	private StudentDao getStudentDao() {
		if (studentDao == null) {
			studentDao = (StudentDao) ApplicationContextUtil.getApplicationContext().getBean("studentDao");
		}
		return studentDao;
	}

	private TeacherDao getTeacherDao() {
		if (teacherDao == null) {
			teacherDao = (TeacherDao) ApplicationContextUtil.getApplicationContext().getBean("teacherDao");
		}
		return teacherDao;
	}

	public ObservableList getStudentItems() {
		return FXCollections.observableArrayList(getStudentDao().findAll());
	}

	public ObservableList getTeacherItems() {
		return FXCollections.observableArrayList(getTeacherDao().findAll());
	}

	public void initAddPane() {
		addStuButton.setOnAction(e -> {
			if (!addStuText.getText().equals("")) {
				try {
					getStudentDao().save(new Student(addStuText.getText().trim()));
					textArea.setText("添加学生：" + addStuText.getText() + "成功");
					addStuText.setText("");
				} catch (Exception e2) {
					textArea.setText("添加学生：" + addStuText.getText() + "失败,该学生已存在");
				}
			} else {
				textArea.setText("请输入数据");
			}
		});
		addTeaButton.setOnAction(e -> {
			if (!addTeaText.getText().equals("")) {
				try {
					getTeacherDao().save(new Teacher(addTeaText.getText().trim()));
					textArea.setText("添加老师：" + addTeaText.getText() + "成功");
					addTeaText.setText("");
				} catch (Exception e2) {
					textArea.setText("添加老师：" + addTeaText.getText() + "失败,该老师已存在");
				}
			} else {
				textArea.setText("请输入数据");
			}
		});
	}

	public void initDelPane() {
		delTab.setOnSelectionChanged(e -> {
			if (delTab.isSelected()) {
				delStuChoice.setItems(getStudentItems());
				delTeaChoice.setItems(getTeacherItems());
			}
		});
		delStuButton.setOnAction(e -> {
			Student student = null;
			if (delStuChoice.getValue() != null) {
				try {
					student = (Student) delStuChoice.getValue();
					getStudentDao().delete(student);
					textArea.setText("删除学生：" + student.getName() + "成功");
					delStuChoice.setValue(null);
					delStuChoice.getItems().clear();
					delStuChoice.setItems(getStudentItems());
				} catch (Exception e2) {
					textArea.setText("删除学生：" + student.getName() + "失败");
				}
			} else {
				textArea.setText("请选择对象");
			}
		});
		delTeaButton.setOnAction(e -> {
			Teacher teacher = null;
			if (delTeaChoice.getValue() != null) {
				try {
					teacher = (Teacher) delTeaChoice.getValue();
					getTeacherDao().delete(teacher);
					textArea.setText("删除老师：" + teacher.getName() + "成功");
					delTeaChoice.setValue(null);
					delTeaChoice.getItems().clear();
					delTeaChoice.setItems(getTeacherItems());
				} catch (Exception e2) {
					textArea.setText("删除老师：" + teacher.getName() + "失败");
				}
			} else {
				textArea.setText("请选择对象");
			}
		});
	}

	public void initModPane() {
		modTab.setOnSelectionChanged(e -> {
			if (modTab.isSelected()) {
				modStuChoice.setItems(getStudentItems());
				modTeaChoice.setItems(getTeacherItems());
			}
		});
		modStuButton.setOnAction(e -> {
			Student student = null;
			if (!modStuText.getText().equals("") && modStuChoice.getValue() != null) {
				try {
					student = (Student) modStuChoice.getValue();
					student.setName(modStuText.getText().trim());
					getStudentDao().update(student);
					textArea.setText("修改学生：" + student.getName() + "成功");
					modStuText.setText("");
					modStuChoice.getItems().clear();
					modStuChoice.setItems(getStudentItems());
				} catch (Exception e2) {
					textArea.setText("删除学生：" + student.getName() + "失败");
				}
			} else {
				textArea.setText("请输入数据和选择对象");
			}
		});
		modTeaButton.setOnAction(e -> {
			Teacher teacher = null;
			if (!modTeaText.getText().equals("") && modTeaChoice.getValue() != null) {
				try {
					teacher = (Teacher) modTeaChoice.getValue();
					teacher.setName(modTeaText.getText().trim());
					getTeacherDao().update(teacher);
					textArea.setText("修改老师：" + teacher.getName() + "成功");
					modTeaText.setText("");
					modTeaChoice.getItems().clear();
					modTeaChoice.setItems(getTeacherItems());
				} catch (Exception e2) {
					textArea.setText("删除老师：" + teacher.getName() + "失败");
				}
			} else {
				textArea.setText("请输入数据和选择对象");
			}
		});
	}

	public void initManPane() {
		manTab.setOnSelectionChanged(e -> {
			if (manTab.isSelected()) {
				manStuChoice.setItems(getStudentItems());
				manTeaChoice.setItems(getTeacherItems());
			}
		});

		addStuTea.setOnAction(e -> {
			Student student = (Student) manStuChoice.getValue();
			Teacher teacher = (Teacher) manTeaChoice.getValue();
			if (student != null && teacher != null) {
				if (student.getTeacher() == null) {
					try {
						student.setTeacher(teacher);
						getStudentDao().update(student);
						teacher.getStudents().add(student);
						textArea.setText("学生" + student.getName() + "添加导师：" + teacher.getName() + "成功");
					} catch (Exception e2) {
						// TODO: handle exception
						textArea.setText("学生" + student.getName() + "添加导师：" + teacher.getName() + "失败");
					}
				} else {
					textArea.setText("学生" + student.getName() + "已有导师：" + student.getTeacher().getName());
				}
			} else {
				textArea.setText("请选择对象");
			}
		});

		updateStuTea.setOnAction(e -> {
			Student student = (Student) manStuChoice.getValue();
			Teacher teacher = (Teacher) manTeaChoice.getValue();
			if (student != null && teacher != null) {
				if (student.getTeacher() != null) {
					try {
						student.setTeacher(teacher);
						getStudentDao().update(student);
						teacher.getStudents().add(student);
						textArea.setText("学生" + student.getName() + "的导师变更为：" + teacher.getName());
					} catch (Exception e2) {
						// TODO: handle exception
						textArea.setText("学生" + student.getName() + "修改导师：" + teacher.getName() + "失败");
					}
				} else {
					textArea.setText("学生：" + student.getName() + "没有导师");
				}
			} else {
				textArea.setText("请选择对象");
			}
		});

		addTeaStu.setOnAction(e -> {
			Student student = (Student) manStuChoice.getValue();
			Teacher teacher = (Teacher) manTeaChoice.getValue();
			if (student != null && teacher != null) {
				if (student.getTeacher() == null) {
					try {
						student.setTeacher(teacher);
						getStudentDao().update(student);
						teacher.getStudents().add(student);
						textArea.setText("导师" + teacher.getName() + "添加学生：" + student.getName() + "成功\n" + "老师:"
								+ teacher.getName() + "的学生为：" + teacher.getStudents());
					} catch (Exception e2) {
						// TODO: handle exception
						textArea.setText("导师" + teacher.getName() + "添加学生：" + student.getName() + "失败\n" + "老师:"
								+ teacher.getName() + "的学生为：" + teacher.getStudents());
					}
				} else {
					textArea.setText("学生" + student.getName() + "已有导师：" + student.getTeacher().getName());
				}
			} else {
				textArea.setText("请选择对象");
			}
		});

		removeTeaStu.setOnAction(e -> {
			Student student = (Student) manStuChoice.getValue();
			Teacher teacher = (Teacher) manTeaChoice.getValue();
			if (student != null && teacher != null) {
				if (student.getTeacher() != null) {
					try {
						student.setTeacher(null);
						getStudentDao().update(student);
						if(teacher.getStudents().remove(student)){
							textArea.setText("导师" + teacher.getName() + "移除学生：" + student.getName() + "成功\n" + "老师:"
									+ teacher.getName() + "的学生为：" + teacher.getStudents());
						}else{
							textArea.setText("导师" + teacher.getName() + "没有学生：" + student.getName()+"，不能移除");
						}
					} catch (Exception e2) {
						// TODO: handle exception
						textArea.setText("导师" + teacher.getName() + "移除学生：" + student.getName() + "失败\n" + "老师:"
								+ teacher.getName() + "的学生为：" + teacher.getStudents());
					}
				} else {
					textArea.setText("学生" + student.getName() + "没有导师");
				}
			} else {
				textArea.setText("请选择对象");
			}
		});

		showStu.setOnAction(e -> {
			Teacher teacher = (Teacher) manTeaChoice.getValue();
			if (teacher != null) {
				textArea.setText("老师:"+ teacher.getName() + "的学生为：" + teacher.getStudents());
			} else {
				textArea.setText("请选择对象");
			}
		});
	}

	public void initialize() {
		getStudentDao();
		getTeacherDao();
		// textArea.setDisable(true);
		initAddPane();
		initDelPane();
		initModPane();
		initManPane();
	}
}
